package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_boat.EntityBeanOc_Boat;
import entitybean.oc_boat_ben.EntityBeanOc_Boat_Ben;
import form.BoatBenForm;
import form.BoatForm;


import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import tools.InterfaceForResultSetToList;
import tools.PageBeanPlus;
import view.ViewOc_Boat;
import view.ViewOc_Boat_Ben;

public class Action_Oc_Boat_Ben extends DispatchAction {
	private Service_Admin service_admin;
	private Service_Public service_public;
	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin serviceAdmin) {
		service_admin = serviceAdmin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public servicePublic) {
		service_public = servicePublic;
	}

	public ActionForward admin_init_oc_boat_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BoatBenForm boatbenform = (BoatBenForm) form;
		System.out.println("初始化海洋环境科考船");
		String sql = "select id,OC_BOATNAME,OC_BOATKIND from OC_BOAT_BEN where DEL='OFF' AND OC_BOATKIND=8 ORDER BY ID DESC";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(boatbenform.getRowcountperpage()), Integer
						.valueOf(boatbenform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Boat_Ben> list = interfaceforresultsettolist
				.ViewOc_Boat_Ben(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("boatbenform", boatbenform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_load_oc_boat_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("载入一个底栖生物科考船信息");
		BoatBenForm boatbenform = (BoatBenForm) form;
		String id = boatbenform.getId();
		EntityBeanOc_Boat_Ben e = this.service_admin
				.Find_Oc_Boat_Ben_By_oc_boatkind_By_id(id);
		request.setAttribute("e", e);
		return mapping.findForward("success");
	}

	public ActionForward admin_add_oc_boat_ben(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("准备添加一个底栖生物科考船信息");

		return mapping.findForward("success");
	}

	public ActionForward admin_insert_oc_boat_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("添加一个底栖生物科考船信息");
		BoatBenForm boatbenform = (BoatBenForm) form;
		Integer ocBoatkind = boatbenform.getOc_boatkind();
		String ocBoatname = boatbenform.getOc_boatname();

		EntityBeanL_Sequence l = this.service_public
				.Find_L_Sequence_By_seq_Name("SEQ_OC_BOAT_BEN");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);
		EntityBeanOc_Boat_Ben e = new EntityBeanOc_Boat_Ben();
		e.setDel("OFF");
		e.setId(id);
		e.setOc_boatkind(ocBoatkind);
		e.setOc_boatname(ocBoatname);
		this.service_public.Update_L_Sequence(l);
		this.service_admin.Insert_Oc_Boat_Ben(e);

		System.out.println("初始化底栖生物境科考船");
		String sql = "select id,OC_BOATNAME,OC_BOATKIND from OC_BOAT_Ben where DEL='OFF' AND OC_BOATKIND=8 ORDER BY ID";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(boatbenform.getRowcountperpage()), Integer
						.valueOf(boatbenform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Boat_Ben> list = interfaceforresultsettolist
				.ViewOc_Boat_Ben(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("boatbenform", boatbenform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_delete_oc_boat_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		System.out.println("删除底栖生物科考船信息");
		BoatBenForm boatbenform = (BoatBenForm) form;
		String id = boatbenform.getId();
		id = this.cut_tail(id);
		this.service_admin.Delete_Oc_Boat_Ben(id);

		String sql = "select id,OC_BOATNAME,OC_BOATKIND from OC_BOAT_BEN where DEL='OFF' AND OC_BOATKIND=8 ORDER BY ID";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(boatbenform.getRowcountperpage()), Integer
						.valueOf(boatbenform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Boat_Ben> list = interfaceforresultsettolist
				.ViewOc_Boat_Ben(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("boatbenform", boatbenform);
		System.out.println("list.size()" + list.size());
		
		
		return mapping.findForward("success");
	}

	public ActionForward admin_update_oc_boat_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("更新一个底栖生物科考船信息");
		BoatBenForm boatbenform = (BoatBenForm) form;
		String oc_boatname = boatbenform.getOc_boatname();
		Integer ocBoatkind = boatbenform.getOc_boatkind();
		String id = boatbenform.getId();
		System.out.println("id=" + id);
		System.out.println("ocBoatkind=" + ocBoatkind);
		System.out.println("oc_boatname=" + oc_boatname);
		EntityBeanOc_Boat_Ben e = this.service_admin
				.Find_Oc_Boat_Ben_By_oc_boatkind_By_id(id);
		e.setOc_boatname(oc_boatname);
		e.setOc_boatkind(ocBoatkind);
		this.service_admin.Update_Oc_Boat_Ben(e);

		String sql = "select id,OC_BOATNAME,OC_BOATKIND from OC_BOAT_BEN where DEL='OFF' AND OC_BOATKIND=8 ORDER BY ID";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(boatbenform.getRowcountperpage()), Integer
						.valueOf(boatbenform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Boat_Ben> list = interfaceforresultsettolist
				.ViewOc_Boat_Ben(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("boatbenform", boatbenform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	public ActionForward admin_search_oc_boat_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		System.out.println("查询底栖生物科考船");
		BoatBenForm boatbenform = (BoatBenForm) form;
		String sql = "select id,OC_BOATNAME,OC_BOATKIND from OC_BOAT_BEN where DEL='OFF' AND OC_BOATKIND=8 ORDER BY ID";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(boatbenform.getRowcountperpage()), Integer
						.valueOf(boatbenform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Boat_Ben> list = interfaceforresultsettolist
				.ViewOc_Boat_Ben(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("boatbenform", boatbenform);
		System.out.println("list.size()" + list.size());

		return mapping.findForward("success");
	}

	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
}
